package com.wejoy.video.middle.station.admin.controller.system;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.wejoy.video.middle.station.admin.controller.BaseController;
import com.wejoy.video.middle.station.admin.util.CurrentUserUtils;
import com.wejoy.video.middle.station.data.annotation.RoleCheck;
import com.wejoy.video.middle.station.data.domain.PageResult;
import com.wejoy.video.middle.station.data.domain.system.EvsUser;
import com.wejoy.video.middle.station.data.domain.system.SysDept;
import com.wejoy.video.middle.station.data.domain.system.SysUser;
import com.wejoy.video.middle.station.data.service.evs.IEvsHttpService;
import com.wejoy.video.middle.station.data.service.system.IEvsUserService;
import com.wejoy.video.middle.station.data.util.WJAssert;
import com.wejoy.video.middle.station.data.vo.RespJSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.ArrayList;

/**
 * evs用户同步
 *
 * @author admin
 * @date 2024年06月29日 17:04:26
 */
@RestController
@RequestMapping("/system/evsUser")
public class EvsUserController extends BaseController {

    @Autowired
    private IEvsUserService evsUserService;
    @Autowired
    private IEvsHttpService evsHttpService;
    /**
     * 保存或更新
     *
     * @param evsUserReq evsUserReq
     * @return RespJSON
     */
    @PostMapping("/save")
    public RespJSON save(@RequestBody EvsUser evsUserReq, @SessionAttribute(CurrentUserUtils.SYSTEM_USER) SysUser loginSysUser) {
        evsUserService.save(evsUserReq, loginSysUser.getUserName());
        return RespJSON.success();
    }

    /**
     * 分页查询
     *
     * @param evsUserReq evsUserReq
     * @return RespJSON
     */
    @PostMapping("/getPageList")
    @RoleCheck(name = "sys_user_list")
    public RespJSON getPageList(@RequestBody EvsUser evsUserReq, @SessionAttribute(CurrentUserUtils.SYSTEM_USER) SysUser loginSysUser) {
        SysDept sysDept = new SysDept();
        sysDept.setParentId(-1);
        sysDept.setIncludeLower(false);
        JSONArray departmentsJson = evsHttpService.getDepartmentsJson(sysDept, loginSysUser.getToken());
        JSONObject users = evsHttpService.getUsers(evsUserReq.getDepartmentId() == null ? departmentsJson.getJSONObject(0).getInteger("id") : evsUserReq.getDepartmentId().intValue(), evsUserReq.getPageNo(), evsUserReq.getPageSize(),
                evsUserReq.getUserName(), evsUserReq.getTel(), loginSysUser.getToken());
        int count = evsUserService.getCount(evsUserReq);
        String data = users.getString("data");
        List<EvsUser> evsUsers = JSONArray.parseArray(data, EvsUser.class);
        PageResult result = new PageResult(evsUserReq.getPageNo(), evsUserReq.getPageSize(), users.getInteger("totalCount"), evsUsers);
        return RespJSON.success(result);
    }

    /**
     * 主键查询
     *
     * @param id id
     * @return RespJSON
     */
    @RequestMapping("/getOneById")
    @RoleCheck(name = "sys_user_list")
    public RespJSON getOneById(Long id) {
        return RespJSON.success(evsUserService.getOneById(id));
    }

    /**
    * 审核
    *
    * @param id id
    * @return RespJSON
    */
    @PostMapping("/audit")
    @ResponseBody
        public RespJSON audit(Long id, @SessionAttribute(CurrentUserUtils.SYSTEM_USER)SysUser loginSysUser) {
        WJAssert.notNull(id, "ID 不合法");
        evsUserService.audit(id, loginSysUser.getUserName());
        return RespJSON.success();
    }

    /**
    * 取消审核
    *
    * @param id id
    * @return RespJSON
    */
    @PostMapping("/unaudit")
    @ResponseBody
        public RespJSON unaudit(Long id, @SessionAttribute(CurrentUserUtils.SYSTEM_USER)SysUser loginSysUser) {
        WJAssert.notNull(id, "ID 不合法");
        evsUserService.unaudit(id, loginSysUser.getUserName());
        return RespJSON.success();
    }

}
